package com.atgouwu.mall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;

@Configuration
public class MyRabbitMQConfig {
    /**
     * 死信队列
     */
    @Bean
    public Queue delayQueue() {
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "test-event-exchange"); //到期后指定交换机
        arguments.put("x-dead-letter-routing-key", "test.release.test"); //到期后指定路由键
        arguments.put("x-message-ttl", 60000); // 消息过期时间 1分钟
        Queue queue = new Queue("test.delay.queue", true, false, false, arguments); //队列名称,是否持久化,是否排他,是否自动删除,属性
        return queue;
    }

    /**
     * 普通队列
     */
    @Bean
    public Queue releaseQueue() {
        Queue queue = new Queue("test.release.test.queue", true, false, false); //队列名称,是否持久化,是否排他,是否自动删除
        return queue;
    }

    /**
     * 主题交换机
     */
    @Bean
    public Exchange eventExchange() {
        return new TopicExchange("test-event-exchange", true, false); //交换机名称,是否持久化,是否自动删除
    }

    /**
     * 死信绑定：死信队列与交换机绑定
     */
    @Bean
    public Binding createBinding() {
        //队列名称,绑定类型,交换机名称,路由键,参数
        return new Binding("test.delay.queue", Binding.DestinationType.QUEUE, "test-event-exchange", "test.create.test", null);
    }

    /**
     * 绑定：普通队列与交换机绑定
     */
    @Bean
    public Binding releaseBinding() {
        //队列名称,绑定类型,交换机名称,路由键,参数
        return new Binding("test.release.test.queue", Binding.DestinationType.QUEUE, "test-event-exchange", "test.release.test", null);
    }

}
